#!/usr/local/bin/ruby
#-*- coding: UTF-8 -*-
require "fac"
class Ordinal
  include Fac
  @N = 0
  def initialize(n)
    @N = n
  end

  def getN
    @N
  end

  def toFacNum(num)
    arr = Array.new(@N,0)
    i = 1
    j = 2
    while num != 0
      e = num%j
      num = num/j
      arr[i] = e
      i += 1
      j += 1
    end
    return arr
  end

  def facToPermutation(facNum)
    arr = Array.new(@N,0)
    pointer = Array(0...@N)
    i = @N -1
    while i >= 0
      p = facNum[i]
      index = pointer[p]
      arr[index] = i+1
      i -= 1
      while p < @N-1
        pointer[p] = pointer[p+1]
        p += 1
      end
    end
    return arr
  end

  def ordinalOrder
    for i in 0...fac(@N)
      arr = facToPermutation(toFacNum(i))
      j = @N-1
      while j >= 0
        print "#{arr[j]}"
        j -= 1
      end
      puts
    end
  end

end
